package com.lujianfei.powersearch.logger.impl;


import com.lujianfei.powersearch.logger.Logger;

/**
 * 描述:
 * 创建者: lujianfei
 * 日期: 2025/5/20
 */

public class JavaLogger implements Logger {
    @Override
    public void d(String tag, String msg) {
        log("debug[" + tag + "] " + msg);
    }

    @Override
    public void e(String tag, String msg, Throwable e) {
        log("error[" + tag + "] " + msg);
    }

    @Override
    public void i(String tag, String msg) {
        log("info[" + tag + "] " + msg);
    }

    @Override
    public void v(String tag, String msg) {
        log("verbose[" + tag + "] " + msg);
    }

    @Override
    public void w(String tag, String msg) {
        log("warn[" + tag + "] " + msg);
    }

    @Override
    public void wtf(String tag, String msg) {
        log("wtf[" + tag + "] " + msg);
    }

    public void log(String msg) {
        int index = 3;
        StringBuilder stringBuilder = new StringBuilder();
        // Print all method calls in the stack frame
        try {
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            String className = stackTrace[index].getClassName();
            String fileName = stackTrace[index].getFileName();
            String methodName = stackTrace[index].getMethodName();
            int lineNumber = stackTrace[index].getLineNumber();

            stringBuilder.append(String.format("%s.%s", className, methodName)).append(String.format("(%s:%s)",fileName, lineNumber));
        } catch (Exception e) {
            System.out.println(e);
        }
        String finalMsg = String.format("%s: %s", stringBuilder, msg);
        // Print clickable link
        System.out.println(finalMsg);
    }
}
